﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using easyList.IO.Writer;
using easyList.Logging;
using easyList.Options.Entities;

namespace easyList.Options
{
    class OptionManager
    {
        //this class contains variables and methods to read and save the program options
        static OptionManager instance = null;
        static readonly object padlock = new object();

        private Logger logger = Logger.Instance;

        private List<Option> options;

        public static OptionManager Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new OptionManager();
                        instance.setOptions(new List<Option>());
                    }
                    return instance;
                }
            }
        }


        public void setOptions(List<Option> options)
        {
            logger.DEBUG("Setting options...");
            this.options = options;
        }

        public Option getOptionByName(String optionName)
        {
            foreach (Option option in options)
            {
               if(option.optionName.Equals(optionName)){
                   return option;
               }
            }
            return null;
        }

        public Option getOptionByName(String optionName, Object defaultValue)
        {
            foreach (Option option in options)
            {
                if (option.optionName.Equals(optionName))
                {
                    return option;
                }
            }
            return new Option(optionName, defaultValue);
        }

        private void loadOptionsFromDisk()
        {
            logger.DEBUG("Reading options from disk...");

        }

        public void writeOptionsToDisk()
        {
            logger.DEBUG("Writing options to disk...");
            string configcontent = "[easyListConfiguration]\r\n";

            foreach (Option option in options)
            {
                configcontent = configcontent + option.optionName + "=" + option.optionValue + "\r\n";
            }

            //write to file
            TXTWriter writer = TXTWriter.Instance;
            writer.writeTXT(configcontent);
        }

    }
}
